<!-- 提现记录 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='16' :sm='24'>
                        <a-form-item label="提现时间">
                            <a-range-picker @change="onDateChange" />
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button style="margin-left: 20px" type='' icon="download" @click="download">导出</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}" size="small">
            <div slot="remark" slot-scope="remark">
                <div>{{remark == '' || remark == null ? '-' : remark}}</div>
            </div>
            <div slot="state" slot-scope="state">
                <div v-for="(item,i) in stateList" :key="item.id">
                    <a-tag v-if="item.id == state" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
        </a-table>

        <!-- 导出数据 -->
        <a-modal
            title="导出数据，每页10000条"
            :visible="downloadVisible"
            @ok="downladHandleOk"
            :confirm-loading="confirmLoading"
            @cancel="downloadVisible = false"
            >
            <div>
                <a-pagination v-model="current" :page-size.sync="pageSize" :total="total" show-less-items />
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { getWithdrawList , downloadAccountWithdrawList } from '@/api/services/account';
export default {
    props: ['accountId'],
    data () {
        return {
            page: 1,
            size: 10,
            dataList: [],
            total: 0,
            isload: false,
            startTime: '',
            endTime: '',
            columns,
            stateList: [
                { id: '1', name: '提现中'},
                { id: '3', name: '提现成功'},
                { id: '4', name: '提现失败'},
                { id: '5', name: '提现异常'},
            ],
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            current: 1,
            downloadVisible: false,
            pageSize: 10000,
            confirmLoading: false,
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 日期筛选
        onDateChange(date,dateStr){
            console.log(date,dateStr)
            if(date.length > 0){
                this.startTime = dateStr[0]
                this.endTime = dateStr[1]
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.getData()
        },
        // 获取数据
        getData(){
            let _this = this
            _this.isload = true;
            const params = {
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'pageSize': _this.size,
                'pageIndex': _this.page,
                'accountId': _this.accountId,
            }
            getWithdrawList(params)
            .then(res => {
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 导出
        download(){
            this.downloadVisible = true;
            console.log('123123')
        },
        downladHandleOk(){
            let _this = this;
            _this.confirmLoading = true;
            const params = {
                'pageSize': _this.pageSize,
                'pageIndex': _this.current,
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'accountId': _this.accountId,
            }
            downloadAccountWithdrawList(params)
            .then(res => {
                _this.confirmLoading = false;
                if(res){
                    const content = res
                    console.log(res,"res")
                    let blob = new Blob([content], {type: 'application/x-excel'});
                    const fileName = `提现记录表.xlsx`
                    if ('download' in document.createElement('a')) { // 非IE下载
                        const elink = document.createElement('a')
                        elink.download = fileName
                        elink.style.display = 'none'
                        elink.href = URL.createObjectURL(blob)
                        document.body.appendChild(elink)
                        elink.click()
                        URL.revokeObjectURL(elink.href) // 释放URL 对象
                        document.body.removeChild(elink)
                    } else { // IE10+下载
                        navigator.msSaveBlob(blob, fileName)
                    }
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>